import {defineStore} from "pinia";
import {IActions, IState} from "@/store/orderModule/types";
import zjRequest from "@/services";

const useOrderModule = defineStore<any, IState, any, IActions>("orderModule", {
    state: () => ({
        currentOrderDetail: {
            status: 5,
            orderNo: "",
            paymentType: 1
        },
        orderListPageInfo: {
            pageSize: 6,
            pageNum: 1,
            pageTotal: 0
        },
        orderList: []
    }),
    actions: {
        async getOrderDetailAction(params) {
            const res = await zjRequest.get({
                url: "/order/detail/" + params.orderNo,
            })
            if (res.code === 0) {
                return res.data
            }
        },
        async setOrderDetailAction(params) {
            if (!params.orderNo) return false
            const data = await this.getOrderDetailAction({
                orderNo: params.orderNo
            })
            if (data) {
                this.currentOrderDetail = data
                console.log(data)
            }
            return !!data
        },
        async getOrderListAction(params) {
            const {needToken, needNavigate, form} = params
            const res = await zjRequest.get({
                url: "/order",
                needToken,
                needNavigate,
                params: {
                    orderByColumn: "create_time",
                    ...form
                }
            })
            if (res.code === 0) {
                return res.data
            }
        },
        async setOrderListAction(params) {
            const {isMoreMode, ...otherParams} = params
            const data = await this.getOrderListAction(otherParams)
            if (data) {
                const {list, pageSize, pageNum, pageTotal} = data
                if (isMoreMode) {
                    this.orderList = [...this.orderList, ...list]
                } else {
                    this.orderList = list
                }
                this.orderListPageInfo = {
                    pageSize,
                    pageNum,
                    pageTotal
                }
            }
            return !!data
        },
        async deleteOrderAction(params) {
            const res = await zjRequest.delete({
                url: `/order/user/${params.orderNo}`,
                needToken: true
            })
            return res.code === 0
        }
    }
})
export default useOrderModule
